package team.nmsg.ge.system.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;



import team.nmsg.auto.TestLog;
import team.nmsg.ge.system.dao.ITestLogDao;


@Component
public class TestLogDaoImpl extends NMSDaoSupport implements ITestLogDao{

	@Override
	public long querylogCount(TestLog businessLog,long startTime,long endTime) {
		
		Session hSession = getHSession();
		
		Criteria criteria = hSession.createCriteria(TestLog.class);
		
		String uName = businessLog.getUser();
		String sn = businessLog.getDevSn();
		String testType = businessLog.getTesttype();
		if( uName!= null && !"".equals(uName) ){
			criteria.add(Restrictions.like("user", "%"+uName+"%"));
		}
		
		if(startTime!=0&&endTime!=0){
			criteria.add(Restrictions.le("timeStamp", endTime));
			criteria.add(Restrictions.ge("timeStamp", startTime));
		}
		
		if(sn!=null&&!"".equals(sn)){
			criteria.add(Restrictions.eq("devSn", sn));
		}
		if(!"-1".equals(testType)){
			if("1".equals(testType)){
				testType = "机框";
			}
			if("2".equals(testType)){
				testType = "主控";
			}
			if("3".equals(testType)){
				testType = "线卡";
			}
			if("4".equals(testType)){
				testType = "电源";
			}
			criteria.add(Restrictions.eq("testtype", testType));
		}
		criteria.setProjection(Projections.rowCount());
		
		List list = criteria.list();
		
		Object object = list.get(0);
		
		try {
			return Long.parseLong(object.toString());
		} catch (Exception e) {
			return 0;
		}
	}

	@Override
	public List<TestLog> QueryTestLogList(int pageSize, int startIndex,
			TestLog businessLog,long startTime,long endTime) {
		
		Session hSession = getHSession();
		Criteria criteria = hSession.createCriteria(TestLog.class);
		criteria.addOrder(Order.desc("timeStamp"));
		String uName = businessLog.getUser();
		String sn = businessLog.getDevSn();
		String testType = businessLog.getTesttype();
		int type = businessLog.getType();
		if( uName!= null && !"".equals(uName) ){
			criteria.add(Restrictions.like("user", "%"+uName+"%"));
		}
		
		if(startTime!=0){
			criteria.add(Restrictions.ge("timeStamp", startTime));
		}
		if(endTime!=0){
			criteria.add(Restrictions.le("timeStamp", endTime));
		}
		if(sn!=null&&!"".equals(sn)){
			criteria.add(Restrictions.eq("devSn", sn));
		}
		if(!"-1".equals(testType)){
			if("1".equals(testType)){
				testType = "机框";
			}
			if("2".equals(testType)){
				testType = "主控";
			}
			if("3".equals(testType)){
				testType = "线卡";
			}
			if("4".equals(testType)){
				testType = "电源";
			}
			criteria.add(Restrictions.eq("testtype", testType));
		}
		if(type!=-1){
			criteria.add(Restrictions.eq("type", type));
		}
		if (pageSize != -1 && startIndex != -1) {
			criteria.setFirstResult(startIndex);
			criteria.setMaxResults(pageSize);
		}
		List list = criteria.list();
		return list;
	}
	
	@Override
	public void deleteLogById(String logId) {
		Session hSession = getHSession();
		Query query = hSession.createSQLQuery("delete from testlog where logId= ?");
		query.setParameter(0, logId);
		query.executeUpdate();
	}
	
	@Override
	public TestLog saveLog(TestLog toSave){
		Session hSession = getHSession();
		Serializable save = hSession.save(toSave);
		TestLog object = (TestLog) hSession.get(TestLog.class, save);
//		hSession.flush();
		return object;
	}
	
	@Override
	public List<TestLog> queryBaseLogsBySn(String sn){
		Session hSession = getHSession();
		Criteria criteria = hSession.createCriteria(TestLog.class);
		criteria.add(Restrictions.eq("devSn", sn));
		criteria.add(Restrictions.eq("type", 1));
		List list = criteria.list();
		return list;
	}
}
